package com.zzhua.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import com.zzhua.processor.FFmpegProcessor;
import com.zzhua.test.M3u8Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
 
@RestController
public class TestController {
 
    /**
     * 目录路径,这个路径需要包含test.info文件，test.key文件和test.mp4文件
     */
    private static final String PATH = "D:\\Projects\\practice\\ffmpeg-demo\\test\\";

    /* 此接口需要先手动生成test.info和test.key在PATH路径下 */
    @PostMapping("uploadToM3u8")
    public void uploadToM3u8() throws Exception {
        FileInputStream inputStream = new FileInputStream(PATH + "test.mp4");

        /* 这里原来的逻辑是
            1、m3u8Url是将生成的m3u8文件流写入的位置，可以填写接收该请求的接口路径
            2、infoUrl是获取keyinfo文件的路径，可以是接口路径
            3、上面2个都可以是本地路径*/
        // String m3u8Url = "http://localhost:8080/upload/test.m3u8";
        // String infoUrl = "http://localhost:8080/preview/test.keyinfo";
        String m3u8Url = "D:\\Projects\\practice\\ffmpeg-demo\\test\\test.m3u8";
        String infoUrl = "D:\\Projects\\practice\\ffmpeg-demo\\test\\test.keyinfo";
        String segmentPattern = "http://localhost:8080/upload/test-%d.ts";
        FFmpegProcessor.convertMediaToM3u8ByHttp(inputStream, m3u8Url, infoUrl, segmentPattern);
    }

    @PostMapping("convertToM3u8")
    public void convertToM3u8(MultipartFile mfile) {
        FFmpegProcessor.convertMediaToM3u8(mfile);
    }

    @Autowired
    private M3u8Component mp3u8Component;

    @PostMapping("convertToM3u8V2")
    public String convertToM3u8V2(MultipartFile mfile) throws Exception {
        return mp3u8Component.mediaFileToJavaM3u8(mfile);
    }


 
    @PostMapping("upload/{fileName}")
    public void upload(HttpServletRequest request, @PathVariable("fileName") String fileName) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        FileWriter writer = new FileWriter(PATH + fileName);
        writer.writeFromStream(inputStream);
        IoUtil.close(inputStream);
    }
 
    /**
     * 预览加密文件
     */
    @PostMapping("preview/{fileName}")
    public void preview(@PathVariable("fileName") String fileName, HttpServletResponse response) throws IOException {
        FileReader fileReader = new FileReader(PATH + fileName);
        fileReader.writeToStream(response.getOutputStream());
    }
 
    /**
     * 预览加密文件
     */
    @GetMapping("download/{fileName}")
    public void download(@PathVariable("fileName") String fileName, HttpServletResponse response) throws IOException {
        FileReader fileReader = new FileReader(PATH + fileName);
        fileReader.writeToStream(response.getOutputStream());
    }
 
}